﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;

namespace University.Workshop.DataAccess
{
    public class TripelDesDataAccess : IDisposable
    {
        public const string ConnString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        private SqlConnection cnn;
        public bool GuardarenBD(TripleDESUtil Des, string username)
        {
            if (Des == null )
            {
                throw new ArgumentException("Error at saving DES like null");
            }
            //ado.net
            cnn = new SqlConnection(ConnString);
            string sqlSentence = @"INSERT INTO [DataBaseHotel].[dbo].[Encrip]([UserName],[IV],[KEY],[Encriptado])VALUES (@username,@IV,@KEY,@Encriptado)";
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandText = sqlSentence;
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@IV",Convert.ToBase64String(Des.IV));
            cmd.Parameters.AddWithValue("@KEY",Convert.ToBase64String( Des.Key));
            cmd.Parameters.AddWithValue("@Encriptado",Convert.ToBase64String(Des.Encriptado));

            try
            {
                cnn.Open();
                int value = cmd.ExecuteNonQuery();
                if (value < 1)
                {
                    throw new ArgumentException("The record wasn't registered");
                }
                else { return true; }

            }
            catch (Exception e)
            {
                throw new ArgumentException("Error saving  Des", e);

            }
            finally
            {
                if (cnn.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }
            }


        }

        public  TripleDESUtil Obtener (string userName)
        {
            {
                //if (date == null) { throw new ArgumentException("Invalid Input Parameters"); }

                cnn = new SqlConnection(ConnString);
                string sqlSentence = @"SELECT * FROM [DataBaseHotel].[dbo].[Encrip] WHERE @username = UserName ";

                SqlCommand cmd = cnn.CreateCommand();
                cmd.Parameters.AddWithValue("@username", userName);
                cmd.CommandText = sqlSentence;
                cmd.CommandType = CommandType.Text;
                try
                {
                    cnn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    String result = null;
                    if (reader.HasRows)
                    {
                        /*while (reader.Read())
                        {
                            result += reader["Name"].ToString() + "\n";
                        }*/
                        reader.Read();
                        string nIV = reader["IV"].ToString();
                        string nkey = reader["KEY"].ToString();
                        string npas = reader["Encriptado"].ToString();
                        return new TripleDESUtil { IV=Convert.FromBase64String(nIV), Key= Convert.FromBase64String(nkey), Encriptado=Convert.FromBase64String(npas) };
                    }
                    else { return null; }
                }
                catch (Exception e)
                {
                    throw new ArgumentException("Error obtaining DES table", e);

                }
                finally
                {
                    if (cnn.State == System.Data.ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                }
            }
        }
        #region Miembros de IDisposable

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (cnn != null)
                {
                    cnn.Dispose();
                    cnn = null;
                }
            }
        }

        #endregion
    }
}
